package com.bitcat.firstapp.ui.activity

import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.edit
import androidx.core.widget.doAfterTextChanged
import com.bitcat.firstapp.app.db.DataBaseHelper
import com.bitcat.firstapp.databinding.ActivityLoginBinding
import es.dmoral.toasty.Toasty

class LoginActivity : AppCompatActivity() {

    private lateinit var binding: ActivityLoginBinding

    private lateinit var dataBaseHelper: DataBaseHelper

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityLoginBinding.inflate(layoutInflater)
        setContentView(binding.root)
//        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
//            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
//            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
//            insets
//        }
        dataBaseHelper = DataBaseHelper(this)


        setupListeners()
    }

    private fun setupListeners() {
        //用户名监听
        binding.usernameInput.doAfterTextChanged {
            if (!it.isNullOrEmpty() && binding.usernameLayout.error != null) {
                binding.usernameLayout.error = null
            }
        }

        //密码监听
        binding.passwordInput.doAfterTextChanged {
            if (!it.isNullOrEmpty() && binding.passwordLayout.error != null) {
                binding.passwordLayout.error = null
            }
        }

        //登录按钮监听
        binding.loginBtn.setOnClickListener {
            login()
        }

        //跳转注册
        binding.jumpRegister.setOnClickListener {
            startActivity(Intent(this, RegisterActivity::class.java))
        }
    }

    private fun login() {
        //获取输入框的内容
        val username = binding.usernameInput.text.toString()
        val password = binding.passwordInput.text.toString()

        //非空校验
        if (username.isEmpty()) {
            binding.usernameLayout.error = "用户名不能为空"
            return
        }
        if (password.isEmpty()) {
            binding.passwordLayout.error = "密码不能为空"
            return
        }

        //密码位数6-12位
        if (password.length < 6 || password.length > 12) {
            binding.passwordLayout.error = "密码位数6-12位"
            return
        }

        //开始登录
        val result = dataBaseHelper.login(username, password)
        if (result) {
            //登录成功

            //缓存登录状态
            val sp = getSharedPreferences("todo", MODE_PRIVATE)
            sp.edit {
                putBoolean("is_login", true)
            }
            Toasty.success(this, "登录成功").show()
            startActivity(Intent(this, MainActivity::class.java))
            finish()
        } else {
            //登录失败
            Toasty.error(this, "用户名或密码错误").show()
        }
    }
}